<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    public function index()
    {
        // 我  要进行聊天了
        $fromid = request('fromid'); // 我的UID
        // 发送给谁
        $toid = request('toid');;  // 要发送的UID

        $data = [
            'fromid'=>$fromid,
            'toid'=>$toid
        ];

        return $data;
    }
    public function saveMessages(){
        $message = request()->all();
        $datas['fromid'] = $message['fromid'];
        $datas['fromname'] = $this->getName($message['fromid']);
        $datas['toid'] = $message['toid'];
        $datas['toname'] = $this->getName($message['toid']);
        $datas['content'] = $message['data'];
        $datas['time'] = $message['time'];
        $datas['isread'] = $message['isread'];
        $datas['type'] = 1;
        DB::table('chat_communication')->insert($datas);
    }
    public function getName($uid){
        $userinfo = DB::table('chat_user')->select('nickname')->find($uid);
        return $userinfo->nickname ?? "";
    }
    public function get_head(){
        $message = request()->all();
        $fromid = $message['fromid'];
        $toid = $message['toid'];
        $from = DB::table('chat_user')->find($fromid);
        $to = DB::table('chat_user')->find($toid);

        return [
            'fromHead'=>$from->headimgurl ?? "",
            'toHead'=>$to->headimgurl ?? "",
        ];
    }
    public function get_name(){
        $message = request()->all();
        $toid = $message['toid'];
        $to = DB::table('chat_user')->find($toid);
        return [
            'toname'=>$to->nickname ?? ""
        ];
    }
    public function message_load(){
        $parameter = request()->all();
        $fromid = $parameter['fromid'];
        $toid = $parameter['toid'];
//        usleep(10000);
        $sql = "(fromid=$fromid and toid=$toid) || (fromid=$toid and toid=$fromid)";
        $message = DB::table('chat_communication as t1')->whereRaw($sql);
        if($message->count()>=10){
            $message->offset($message->count() - 10)->limit(10);
        }

        return $message->get();
    }
    # 上传图片，返回图片地址
    public function uploadimg(){
        $file = $_FILES['file'];
        $fromid = request("fromid");
        $toid = request("toid");
        $online = request("online");

        // 截取文件名称
        $suffix = strtolower(strrchr($file['name'],'.'));
        $type = [".jpg",".jpeg",".gif",".png"];
        if(!in_array($suffix,$type)){
            return ["status"=>"img type error"];
        }
        // 图片不能大于 5 兆
        if($file['size']/1024>5120){
            return ["status"=>"img is too large"];
        }
        // uniqid() 函数基于以微秒计的当前时间，生成一个唯一的 ID。
        $filename = uniqid("chat_img_",false);
        // 获取文件上传路径
        $uploadpath = public_path()."\\uploads\\";
        $file_up = $uploadpath.$filename.$suffix;
        /*
         * move_uploaded_file() 函数把上传的文件移动到新位置。
         * 如果成功该函数返回 TRUE，如果失败则返回 FALSE。
         */
        $re = move_uploaded_file($file['tmp_name'],$file_up);
        if($re){
            $fromname = DB::table('chat_user')->find($fromid);
            $toname = DB::table('chat_user')->find($toid);

            $name = $filename.$suffix;
            $data['content'] = $name;
            $data['fromid'] = $fromid;
            $data['toid'] = $toid;
            $data['fromname'] = $fromname->nickname ?? "";
            $data['toname'] = $toname->nickname ?? "";
            $data['time'] = time();
            $data['isread'] = $online;
            $data['type'] = 2;

            $message_id = DB::table('chat_communication')->insert($data);
            if($message_id){
                return ['status'=>'ok','img_name'=>$name];
            }else{
                return ['status'=>'false'];
            }
        }

    }
}
